<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="className">
        <el-input v-model="queryParams.className" placeholder="请输入课程名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- 下拉框 -->
      <el-form-item label="教练" prop="coachId" style="margin-left: -20px;">
        <el-select v-model="queryParams.coachId" placeholder="请选择教练">
          <el-option v-for="item in teacherList" :key="item.id" :label="item.tName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="场馆" prop="gymId" style="margin-left: -20px;">
        <el-select v-model="queryParams.gymId" placeholder="请选择场馆">
          <el-option v-for="item in gymList" :key="item.id" :label="item.gymName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="开始时间">
        <el-date-picker v-model="daterangeStartTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker v-model="daterangeEndTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['keep:teamClass:add']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['keep:teamClass:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamClassList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="课程名称" align="center" prop="className" />
      <el-table-column label="课程图片" align="center" prop="classImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.classImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="教练名称" align="center" prop="teacher.tName" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="可预约人数" align="center" prop="maxAttendees" />
      <el-table-column label="开课最低人数" align="center" prop="minAttendees" />
      <el-table-column label="课程现价" align="center" prop="currentPrice" />
      <el-table-column label="课程原价" align="center" prop="originalPrice" />
      <el-table-column label="课程介绍" align="center" prop="description" />
      <el-table-column label="健身馆名称" align="center" prop="fitnessGym.gymName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['keep:teamClass:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['keep:teamClass:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改团课课程管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @open="getGym">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="课程名称" prop="className">
          <el-input v-model="form.className" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程图片" prop="classImage">
          <image-upload v-model="form.classImage" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="可预约人数" prop="maxAttendees">
          <el-input v-model="form.maxAttendees" placeholder="请输入可预约人数" />
        </el-form-item>
        <el-form-item label="开课最低人数" prop="minAttendees">
          <el-input v-model="form.minAttendees" placeholder="请输入开课最低人数" />
        </el-form-item>
        <el-form-item label="场馆">
          <el-select v-model="form.gymId" placeholder="请选择场馆" @change="getTeachers">
            <el-option v-for="item in gymList" :key="item.id" :label="item.gymName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教练">
          <el-select v-model="form.tId" placeholder="请选择教练">
            <el-option v-for="item in gymListTo" :key="item.id" :label="item.tName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程现价" prop="currentPrice">
          <el-input v-model="form.currentPrice" placeholder="请输入课程现价" />
        </el-form-item>
        <el-form-item label="课程原价" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入课程原价" />
        </el-form-item>
        <el-form-item label="课程介绍" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listTeamClass,
    getTeamClass,
    delTeamClass,
    addTeamClass,
    updateTeamClass
  } from "@/api/keep/teamClass";

  import {
    listTeacher,
    getTeacher,
    addTeacher,
    updateTeacher,
    delTeacher
  } from "@/api/keep/teacher";

  import {
    listGym,
    getGym,
    addGym,
    updateGym,
    delGym
  } from "@/api/keep/gym"
import { createLogger } from "vuex";

  export default {
    name: "TeamClass",
    data() {
      return {
        // 下拉框选项
        teacherList: [],
        // 教练下拉框
        gymList: [],

        tId: "",

        gymListTo: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 团课课程管理表格数据
        teamClassList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 健身馆id时间范围
        daterangeStartTime: [],
        // 健身馆id时间范围
        daterangeEndTime: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          className: null,
          coachId: null,
          startTime: null,
          endTime: null,
          gymId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          className: [{
            required: true,
            message: "课程名称不能为空",
            trigger: "blur"
          }],
          coachId: [{
            required: true,
            message: "教练id不能为空",
            trigger: "change"
          }],
          startTime: [{
            required: true,
            message: "开始时间不能为空",
            trigger: "blur"
          }],
          endTime: [{
            required: true,
            message: "结束时间不能为空",
            trigger: "blur"
          }],
          maxAttendees: [{
            required: true,
            message: "可预约人数不能为空",
            trigger: "blur"
          }],
          minAttendees: [{
            required: true,
            message: "开课最低人数不能为空",
            trigger: "blur"
          }],
          currentPrice: [{
            required: true,
            message: "课程现价不能为空",
            trigger: "blur"
          }],
          gymId: [{
            required: true,
            message: "健身馆id不能为空",
            trigger: "change"
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getTeacher();
      this.getGym();
    },
    methods: {
      /** 查询团课课程管理列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeStartTime && '' != this.daterangeStartTime) {
          this.queryParams.params["beginStartTime"] = this.daterangeStartTime[0];
          this.queryParams.params["endStartTime"] = this.daterangeStartTime[1];
        }
        if (null != this.daterangeEndTime && '' != this.daterangeEndTime) {
          this.queryParams.params["beginEndTime"] = this.daterangeEndTime[0];
          this.queryParams.params["endEndTime"] = this.daterangeEndTime[1];
        }
        listTeamClass(this.queryParams).then(response => {
          this.teamClassList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 获取老师数据
      getTeacher() {
        this.loading = true;
        listTeacher(this.queryParams).then(response => {
          this.teacherList = response.rows;

          this.loading = false;
        });
      },
      // 获取场馆数据
      getGym() {
        this.loading = true;
        listGym(this.queryParams).then(response => {
          this.gymList = response.rows;

          this.loading = false;
        });
      },
      getTeachers() {
        this.loading = true;
        listGym({
          id: this.form.gymId
        }).then(response => {
          // console.log(response.rows);
          this.gymListTo = response.rows[0].teacher;

          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          className: null,
          classImage: null,
          coachId: null,
          startTime: null,
          endTime: null,
          maxAttendees: null,
          minAttendees: null,
          currentPrice: null,
          originalPrice: null,
          description: null,
          gymId: null,
          del: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeStartTime = [];
        this.daterangeEndTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加团课课程管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getTeamClass(id).then(response => {
          this.form = response.data;
          console.log(response.data.teacher.id,"+++++++++++++");
          this.tId = response.data.teacher.id;
          console.log(this.tId,"__________________");
          this.open = true;
          this.title = "修改团课课程管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateTeamClass(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addTeamClass(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除团课课程管理编号为"' + ids + '"的数据项？').then(function() {
          return delTeamClass(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('keep/teamClass/export', {
          ...this.queryParams
        }, `teamClass_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
